package com.sun.service.test;


import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.http.HttpEntity;
import sun.net.www.http.HttpClient;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DownLoadPrintFileController {

    //测试
    public static String ipAdress = "http://47.250.136.195:1011";

    //生产
//    public static String ipAdress = "https://myfactory.cheryinternational.com";


    public static String filePath = "F:\\download\\";

    public static String loginUrl = ipAdress+"/gateway/system/api/user/login";

    public static String downloadUrl = ipAdress+"/gateway/print/api/print/downloadPrintFile";

    public static String uploadUrl = ipAdress+"/gateway/print/api/printDemo/upload";

    @AllArgsConstructor
    public static class LoginEntity {
        public String loginName;
        public String password;
    }


    /**
     * 打印模板文件批量下载到本地
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        String token = login();
        List<DataJson> beanList = JSONUtil.toList(dataJson, DataJson.class);
        for (DataJson x : beanList) {
            if (x.getTemplate_path().startsWith("persistence")){
                download(token, x);
            }
        }
    }

    /**
     * 打印文件批量从本地上传到服务器
     *
     * @param args
     */
    public static void main2(String[] args) {
        String token = login();
        List<DataJson> beanList = JSONUtil.toList(dataJson, DataJson.class);
        Map<String, String> map = new HashMap<>();
        for (DataJson json : beanList) {
            File file = new File("F:\\download\\" + json.template_code + ".jasper");
            HttpResponse httpResponse = HttpUtil
                    .createPost(uploadUrl)
                    .header("Authorization", token)
                    .form("file", file)
                    .execute();
            String body = httpResponse.body();
            map.put(json.getTemplate_code(), JSONUtil.parseObj(body).getByPath("data.groupName").toString() + "/" + JSONUtil.parseObj(body).getByPath("data.remoteFileName").toString());
        }
        System.out.println("map = " + JSONUtil.toJsonPrettyStr(map));
    }

    /**
     * Excel模板批量导入到服务器中
     *
     * @param args
     */
    public static void main3(String[] args) {
        String token = login();
        List<DataJson> beanList = JSONUtil.toList(dataJson, DataJson.class);
        Map<String, String> map = new HashMap<>();
        for (DataJson json : beanList) {
            File file = new File("F:\\SVN\\马来\\导入模板" + json.template_code + ".jasper");
            HttpResponse httpResponse = HttpUtil
                    .createPost(uploadUrl)
                    .header("Authorization", token)
                    .form("file", file)
                    .execute();
            String body = httpResponse.body();
            map.put(json.getTemplate_code(), JSONUtil.parseObj(body).getByPath("data.groupName").toString() + "/" + JSONUtil.parseObj(body).getByPath("data.remoteFileName").toString());
        }
        System.out.println("map = " + JSONUtil.toJsonPrettyStr(map));
    }

    public static String login() {
        HttpRequest request = new HttpRequest(loginUrl);
        HttpResponse response = request.setConnectionTimeout(5000)
                .method(Method.POST)
                .body(JSONUtil.toJsonStr(MapUtil.builder().put("loginName", "hxy0098").put("password", "Hxy_0098").build()))
                .execute();
        return JSONUtil.parseObj(response.body()).getByPath("data.access_token").toString();

    }

    public static void download(String token, DataJson dataJson) throws IOException {
        String downloadPath = dataJson.template_path.replace("persistence/","");
        String fileName = dataJson.getTemplate_code() + ".jasper";
        HttpRequest request = new HttpRequest(downloadUrl + "?fileName=" + fileName + "&groupName=persistence&remoteFileName=" + downloadPath);
        HttpResponse response = request.setConnectionTimeout(5000)
                .setMethod(Method.GET)
                .header("Authorization", token)
                .execute();
        OutputStream outputStream = new FileOutputStream(filePath + fileName);

        String responseContentType = response.header("Content-Type");
        String errMsg = fileName+"下载失败:";
        if(responseContentType==null){
            System.out.println(errMsg+"读取响应头异常,ContentType为空");
            return;
        }else if(responseContentType.contains("application/json")){
            System.out.println(errMsg+JSONUtil.parseObj(response.body()).getByPath("msg").toString());
            return;
        }else if(responseContentType.contains("application/octet-stream")){
            System.out.println(fileName+"下载成功");
        }else{
            System.out.println(errMsg+"读取响应头异常");
            return;
        }

        InputStream inputStream = response.bodyStream();
        byte[] bytes = new byte[1024];
        int byteIndex;
        while ((byteIndex = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, byteIndex);
        }
        inputStream.close();
        outputStream.close();
    }

    @Data
    public static class DataJson {
        private String template_code;
        private String template_path;
    }

    private static final String dataJson = "[\n" +
            "        {\n" +
            "                \"template_code\" : \"partCode\",\n" +
            "                \"template_path\" : \"persistence\\/20240329\\/default\\/73d15d9db5b6402e9990cc4e6d71df9c.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"optionalCode\",\n" +
            "                \"template_path\" : \"persistence\\/20240401\\/default\\/d1f0fe50c0214465ad50e0a0a03fe9e3.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"vinTZMialCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/46\\/CgICkmQMoD2ADAToAABdeui27SE.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"cocCode1\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/00\\/CgICk2PIs6aARvEZAAGmMWqaKBE.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"dpdSheet\",\n" +
            "                \"template_path\" : \"persistence\\/20240401\\/default\\/eb38c5a8b8ed423584ce76b548323daa.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"barcode\",\n" +
            "                \"template_path\" : \"persistence\\/20240401\\/default\\/13cb56f2bb29459c949add37a3d6cade.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"optionalCode2\",\n" +
            "                \"template_path\" : \"persistence\\/20240329\\/default\\/ac02347a0d834cb5aaef1500687df14a.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"submitOrderCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/00\\/CgICkmPItKGAN4FmAADzZcZyFYU.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"jitSheet\",\n" +
            "                \"template_path\" : \"persistence\\/default\\/20240403\\/e4e22e5d30974afabba54b3537607d8b.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"sxCard\",\n" +
            "                \"template_path\" : \"persistence\\/20240401\\/default\\/18a004968a7440319a52cd54af7d2c03.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"photoCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/2E\\/CgICkmQFOZeAFqZTAABRbp8iOpU.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"materialCode\",\n" +
            "                \"template_path\" : \"persistence\\/20240401\\/default\\/5ee6766b180d4d2bbba195b51d22021c.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"vinMarkCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/11\\/CgICkmPq35OAL-gFAABemYardps.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"exportMarkCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/07\\/14\\/CgICk2RKHiOAU7yrAABkRbjBVwI.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"vehicleConfigCode\",\n" +
            "                \"template_path\" : \"persistence\\/20240418\\/default\\/8ac456683397493aa7295a9a44e160c7.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"kdCarton\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/00\\/CgICkmPKE86AdqwrAACCbF_gkgY.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"engineSubCode\",\n" +
            "                \"template_path\" : \"persistence\\/20240401\\/default\\/4a4e15a5c2704783b11229d984e1e166.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"envListCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/25\\/CgICkmP9v4uAXmFsAAECmBVYmbU.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"fuelCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/25\\/CgICk2P9vfSACrM9AACvHyqiUeY.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"locationOneCode\",\n" +
            "                \"template_path\" : \"persistence\\/20240327\\/default\\/5ce01a2e3c5644b8ac9cdf9f89e528ea.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"newCarOfflineReport\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/00\\/CgICk2PItDqAbt0OAAG12AFOUMU.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"kanban\",\n" +
            "                \"template_path\" : \"persistence\\/20240327\\/default\\/ba7d385ef99b4b3aa0589ee4adddcdfd.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"instrumentCode\",\n" +
            "                \"template_path\" : \"persistence\\/20240329\\/default\\/7c5c604aae5f46ed8d19fdf966c7b643.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"outbTask\",\n" +
            "                \"template_path\" : \"persistence\\/default\\/20240403\\/3e59e8aa725d4e4f9f4fe213125dc47a.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"locationTwoCode\",\n" +
            "                \"template_path\" : \"persistence\\/20240327\\/default\\/b76e43e833ad406281bb9bee6b904854.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"carFrameCode\",\n" +
            "                \"template_path\" : \"persistence\\/20240329\\/default\\/bebc447e3b8241ee8b6704360c4690d1.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"backshaftCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/05\\/B6\\/CgICkmRBAWyAQ-GvAADQ8ESlqnI.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"ccapCocCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/05\\/4D\\/CgICkmQ-XzKAWXfSAAH4trBYKPY.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"vehicleImageCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/00\\/CgICkmPItMqAM5WgAAMHc7psQbo.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"leftDoorCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/20\\/CgIFvmRwcNmARvgjAADmugjrV60.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"rightDoorCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/00\\/CgICk2PItJCAGAsHAADPD3n8f3Q.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"receivePackSerialLatePrint\",\n" +
            "                \"template_path\" : \"persistence\\/20240327\\/default\\/232f490fc15b4632921c90bcbeb3a645.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"receivePackSerialPrint\",\n" +
            "                \"template_path\" : \"persistence\\/20240327\\/default\\/d0302d22d0df4b93b5fb6357209e218a.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"sanReCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/02\\/01\\/CgICk2Qie2-ACaO6AABP0hH5TW4.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"qianHuaZhuCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/02\\/01\\/CgICk2Qie2-ACaO6AABP0hH5TW4.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"houZhouCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/02\\/01\\/CgICk2Qie2-ACaO6AABP0hH5TW4.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"specialExportMarkCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/02\\/0B\\/CgICk2Qirx6ARXTfAABQGhgcxJY.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"inbTask\",\n" +
            "                \"template_path\" : \"persistence\\/20240327\\/default\\/28d2e475f52543a58b39f01848b93360.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"taskPartLabel\",\n" +
            "                \"template_path\" : \"persistence\\/20240327\\/default\\/2a95d13e27184bb9a70b233b0ef0e220.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"taskSerialLabel\",\n" +
            "                \"template_path\" : \"persistence\\/20240327\\/default\\/e7c78c447dc64316816731bda982f96d.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"phevEnvListCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/07\\/32\\/CgICkmRLJGyAJekCAAESK9TMMt4.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"hybridEnergyMarkCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/07\\/17\\/CgICk2RKKFCANNX1AADGT-3sEr0.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"softlabelCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/1C\\/CgIFvmRbW5uARffIAABnO_VIEhg.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"fuelCodeBindUser\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/00\\/CgICkmPKIOSAGRJ3AAByJVwh2yA.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"frontStrutCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/20\\/CgIFvmRt2QaAXJzaAADQ81LWUME.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"frontBumperCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/20\\/CgIFvmRt2RmAD2mNAADQ6q_Fsv8.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"rearBumperCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/20\\/CgIFvmRt2SqAAdYzAADQ6sLFlL4.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"largePartCode\",\n" +
            "                \"template_path\" : \"persistence\\/20240401\\/default\\/8b9b962d14c34f9e8004d129c9741d68.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"offlineSettleOrder\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/28\\/CgIFvmTAuU6AQ25kAAC6rmBRUwU.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"houQuCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/76\\/CgIFvmULmOWAKsYhAABTGUKeuGQ.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"electricEnergyCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/77\\/CgIFvmUTiU2AUGtHAACjK_yimBQ.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"electricEnvListCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/77\\/CgIFvmUTiU2AUGtHAACjK_yimBQ.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"phevNewCarOfflineReport\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/77\\/CgIFvmUU37WADBmFAAHi17XAXl8.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"heatPumpCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/23\\/CgIFvmR6nO6AIIZEAADRR-IU_Uw.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"cduCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/23\\/CgIFvmR6nO6AIIZEAADRR-IU_Uw.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"cgbomVehicleConfigCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/03\\/CgQKZ2WON4-ASvXKAADiBrC-1C4.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"partCode3\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/03\\/CrwDN2Wk2VGAIiuJAAOQkkC7azU.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"rearElectricMotorCode\",\n" +
            "                \"template_path\" : \"group1\\/M00\\/00\\/44\\/CrwDNmXhKFuAVnoRAADRVGg6aB0.jasper\"\n" +
            "        },\n" +
            "        {\n" +
            "                \"template_code\" : \"vtaCode\",\n" +
            "                \"template_path\" : \"persistence\\/20240401\\/default\\/00a37b4c99c04c6790684bb0a6e2604a.jasper\"\n" +
            "        }\n" +
            "]";

}
